﻿using System;
using System.Data;
using System.Windows.Forms;

namespace SiparisKontrol
{
    public enum AramaSekli
    {
        Kodu, Adi
    }

    public partial class frmListe : Form
    {
        public string Filtre { get; set; }
        public AramaSekli AramaTipi { get; set; }
        public string[] Result { get; set; }

        public frmListe()
        {
            InitializeComponent();
        }

        private void frmListe_Load(object sender, EventArgs e)
        {
            using (Query q = new Query())
            {
                if (AramaTipi == AramaSekli.Kodu)
                {
                    q.Add("SELECT     cari_kod AS Kodu, cari_unvan1 + ' ' + cari_unvan2 AS Ünvanı FROM         CARI_HESAPLAR WHERE     (cari_kod like @prm +'%')");
                    q.Add("@prm", SqlDbType.NVarChar, Filtre.Replace('*', '%'), 25);
                }
                else
                {
                    q.Add("SELECT     cari_kod AS Kodu, cari_unvan1 + ' ' + cari_unvan2 AS Ünvanı FROM         CARI_HESAPLAR WHERE  (cari_unvan1 + ' ' + cari_unvan2 like @prm +'%')");
                    q.Add("@prm", SqlDbType.NVarChar, Filtre.Replace('*', '%'), 25);
                }
                gridControl1.DataSource = q.Fill();
                gridView1.BestFitColumns();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Sec();
        }

        private void Sec()
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                string[] sonuc = new string[2];
                sonuc[0] = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                sonuc[1] = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                this.Result = sonuc;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Abort;
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            Sec();
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Sec();
            }
        }
    }
}
